﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using MiniFox.Data.EntityFramework;
using MiniFox.Data.EntityFramework.MySql;

namespace Biz.Data.Entities;

public partial class SampleDbContext : MySqlDbContext
{
    public SampleDbContext(DbContextOptions<SampleDbContext> options)
        : base("SampleDbContext", options)
    {
    }

    public virtual DbSet<DeviceInfo> DeviceInfo { get; set; }

    public virtual DbSet<OrderInfo> OrderInfo { get; set; }

    public virtual DbSet<OrderItemInfo> OrderItemInfo { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .UseCollation("utf8mb4_0900_ai_ci")
            .HasCharSet("utf8mb4");

        modelBuilder.Entity<DeviceInfo>(entity =>
        {
            entity.HasKey(e => e.DeviceId).HasName("PRIMARY");

            entity.ToTable("device_info", tb => tb.HasComment("设备资料数据"));

            entity.Property(e => e.DeviceId)
                .HasMaxLength(50)
                .HasComment("唯一id")
                .HasColumnName("device_id");
            entity.Property(e => e.CreateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("create_time");
            entity.Property(e => e.DeviceName)
                .HasMaxLength(50)
                .HasComment("设备名称")
                .HasColumnName("device_name");
            entity.Property(e => e.DeviceType)
                .HasMaxLength(50)
                .HasComment("设备类型")
                .HasColumnName("device_type");
            entity.Property(e => e.DisplayCode)
                .HasMaxLength(30)
                .HasComment("自编码")
                .HasColumnName("display_code");
            entity.Property(e => e.Enabled)
                .IsRequired()
                .HasDefaultValueSql("'1'")
                .HasComment("启用状态")
                .HasColumnName("enabled");
            entity.Property(e => e.SnCode)
                .HasMaxLength(100)
                .HasComment("出厂序列码")
                .HasColumnName("sn_code");
            entity.Property(e => e.UpdateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("update_time");
        });

        modelBuilder.Entity<OrderInfo>(entity =>
        {
            entity.HasKey(e => e.OrderId).HasName("PRIMARY");

            entity.ToTable("order_info");

            entity.Property(e => e.OrderId)
                .HasMaxLength(50)
                .HasColumnName("order_id");
            entity.Property(e => e.Buyer)
                .HasMaxLength(50)
                .HasComment("买方")
                .HasColumnName("buyer");
            entity.Property(e => e.CreateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("create_time");
            entity.Property(e => e.Paid)
                .HasComment("已付")
                .HasColumnName("paid");
            entity.Property(e => e.PayMode)
                .HasMaxLength(100)
                .HasComment("付款方式")
                .HasColumnName("pay_mode");
            entity.Property(e => e.Seller)
                .HasMaxLength(50)
                .HasComment("卖方")
                .HasColumnName("seller");
            entity.Property(e => e.TotalAmount)
                .HasPrecision(10, 4)
                .HasComment("订单总额")
                .HasColumnName("total_amount");
            entity.Property(e => e.TradeAmount)
                .HasPrecision(10, 4)
                .HasComment("实际交易额")
                .HasColumnName("trade_amount");
            entity.Property(e => e.TradeTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasComment("交易时间")
                .HasColumnType("timestamp")
                .HasColumnName("trade_time");
        });

        modelBuilder.Entity<OrderItemInfo>(entity =>
        {
            entity.HasKey(e => new { e.OrderId, e.Ordinal })
                .HasName("PRIMARY")
                .HasAnnotation("MySql:IndexPrefixLength", new[] { 0, 0 });

            entity.ToTable("order_item_info", tb => tb.HasComment("订单项，该表为子表类型，绝大部分业务场景中随主表更新时做全删全加，不可单独 insert \\ update \\ delete"));

            entity.Property(e => e.OrderId)
                .HasMaxLength(50)
                .HasComment("订单号")
                .HasColumnName("order_id");
            entity.Property(e => e.Ordinal).HasColumnName("ordinal");
            entity.Property(e => e.GoodsName)
                .HasMaxLength(100)
                .HasComment("商品")
                .HasColumnName("goods_name");
            entity.Property(e => e.Quantity)
                .HasComment("数量")
                .HasColumnName("quantity");
            entity.Property(e => e.TaxRate)
                .HasComment("税率")
                .HasColumnName("tax_rate");
            entity.Property(e => e.TradeAmount)
                .HasPrecision(10, 4)
                .HasComment("实际交易额")
                .HasColumnName("trade_amount");
            entity.Property(e => e.Unit)
                .HasMaxLength(20)
                .HasComment("计量单位名称")
                .HasColumnName("unit");
            entity.Property(e => e.UnitPrice)
                .HasPrecision(10, 4)
                .HasComment("单价")
                .HasColumnName("unit_price");

            entity.HasOne(d => d.Order).WithMany(p => p.OrderItemInfo)
                .HasForeignKey(d => d.OrderId)
                .HasConstraintName("order_item_info_order_info_FK");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
